package com.example.shorg.controller;

import com.example.shorg.dataobject.ProductCategory;
import com.example.shorg.dataobject.ProductInfo;
import com.example.shorg.service.CategoryService;
import com.example.shorg.service.ProductService;
import com.example.shorg.service.impl.ProductSericeImpl;
import com.example.shorg.utils.ResultVOUtils;
import com.example.shorg.vo.ProductInfoVO;
import com.example.shorg.vo.ProductVO;
import com.example.shorg.vo.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/buyer/product")
public class BuyerProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private CategoryService categoryService;

    @RequestMapping("/list")
    public ResultVO list(){

        //1、查询所有的上架商品
        List<ProductInfo> productInfoList = productService.findUpAll();

        //2、查询类目
        ArrayList<Integer> typeList = new ArrayList<>();
        for(ProductInfo info : productInfoList){
            typeList.add(info.getCategoryType());
        }
        List<ProductCategory> productCategoryList = categoryService.findByCategoryTypeIn(typeList);
        //3、数据拼装

        List<ProductVO> productVOList = new ArrayList<>();
        for(ProductCategory productCategory: productCategoryList){
            ProductVO productVO = new ProductVO();
            productVO.setCategoryName(productCategory.getCategoryName());
            productVO.setCategoryType(productCategory.getCategoryType());

            List<ProductInfoVO> productInfoVOList = new ArrayList<>();
            for(ProductInfo productInfo: productInfoList){
                if(productInfo.getCategoryType().equals(productCategory.getCategoryType())){
                    ProductInfoVO productInfoVO = new ProductInfoVO();
                    BeanUtils.copyProperties(productInfo, productInfoVO);
                    productInfoVOList.add(productInfoVO);
                }
            }
            productVO.setProductInfoVOList(productInfoVOList);
            productVOList.add(productVO);
        }


//        ResultVO resultVO = new ResultVO();
//        resultVO.setCode(11);
//        resultVO.setMsg("hahha ");
//        resultVO.setData(productVOList);
//        return resultVO;
        return ResultVOUtils.success(productVOList);
    }
}
